package com.bigpillar.orderservice.service.impl;

import com.bigpillar.orderservice.entity.Order;
import com.bigpillar.orderservice.mapper.OrderMapper;
import com.bigpillar.orderservice.service.AccountService;
import com.bigpillar.orderservice.service.OrderService;
import com.bigpillar.orderservice.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl  implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;
    @Override

    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class) // 发生任何异常都回滚
    public void create(Order order) {
        // 1 建订单
        orderMapper.create(order);

        // 2 减库存
        storageService.decrease(order.getProductId(), order.getCount());

        // 3 扣余额
        accountService.decrease(order.getUserId(), order.getMoney());

        // 4 改订单状态,从0到1,1代表已完成
        orderMapper.update(order.getUserId(), 0);

    }
}
